package com.hx.zhg.web.controller.system;

import com.hx.zhg.service.system.WxSystemService;
import com.hx.zhg.service.system.vo.JSSDKConfigVo;
import com.hx.zhg.util.HttpClientUtil;
import com.hx.zhg.util.JsonUtils;
import com.hx.zhg.web.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by zhenghegong on 2020/4/17.
 */
@Api(tags = "微信系统相关接口")
@RestController
@RequestMapping("/wxSystem")
public class WxSystemController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(WxSystemController.class);

    @Autowired
    private WxSystemService wxSystemService;

    @ApiOperation(value = "通过URL获取jssdkconfig", notes = "通过URL获取jssdkconfig")
    @RequestMapping(value = "/getConfigByUrl", method = RequestMethod.GET)
    public String checkToken(String url, HttpServletRequest request) {
        String result = "";
        try {
            log.info(String.format("【*】获取JSSDK_CONFIG：param->url:【%s】", url));
            JSSDKConfigVo jssdkConfig = wxSystemService.getJSSDKConfig(url);
            if (jssdkConfig != null) {
                result = JsonUtils.objToJson(jssdkConfig);
            }
        } catch (Exception e) {
            log.error(String.format("system throw exception error: %s; HEADERS: %s", getHeaders(request), e));
            return this.retInternalErrorDefault();
        }
        return result;
    }

    @ApiOperation(value = "获取TOKEN(缓存)", notes = "(从缓存中获取)获取TOKEN")
    @RequestMapping(value = "/getToken", method = RequestMethod.GET)
    public String getToken(HttpServletRequest request) {
        String result = "";
        try {
            result = wxSystemService.getCacheAccessToken();
        } catch (Exception e) {
            log.error(String.format("system throw exception error: [%s]; HEADERS: %s", e, getHeaders(request)));
            return this.retInternalErrorDefault();
        }
        return result;
    }



}
